
package com.cmc.dao.hibernate;

import java.util.List;

import com.cmc.dao.hibernate.BaseDaoHibernate;
import com.cmc.model.Generalsetting;
import com.cmc.dao.GeneralsettingDao;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;

public class GeneralsettingDaoHibernate extends BaseDaoHibernate implements GeneralsettingDao {

    /**
     * @see com.cmc.dao.GeneralsettingDao#getGeneralsettings(com.cmc.model.Generalsetting)
     */
    public List getGeneralsettings(final Generalsetting generalsetting) {
        //return getHibernateTemplate().find("from Generalsetting");

        /* Remove the line above and uncomment this code block if you want 
           to use Hibernate's Query by Example API.*/
        if (generalsetting == null) {
            return getHibernateTemplate().find("from Generalsetting");
        } else {
            // filter on properties set in the generalsetting
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(generalsetting).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Generalsetting.class).add(ex).list();
                }
            };
            return (List) getHibernateTemplate().execute(callback);
        }
    }

    /**
     * @see com.cmc.dao.GeneralsettingDao#getGeneralsetting(Long id)
     */
    public Generalsetting getGeneralsetting(final Long id) {
        Generalsetting generalsetting = (Generalsetting) getHibernateTemplate().get(Generalsetting.class, id);
        if (generalsetting == null) {
            log.warn("uh oh, generalsetting with id '" + id + "' not found...");
            throw new ObjectRetrievalFailureException(Generalsetting.class, id);
        }

        return generalsetting;
    }

    /**
     * @see com.cmc.dao.GeneralsettingDao#saveGeneralsetting(Generalsetting generalsetting)
     */    
    public void saveGeneralsetting(final Generalsetting generalsetting) {
        getHibernateTemplate().saveOrUpdate(generalsetting);
    }

    /**
     * @see com.cmc.dao.GeneralsettingDao#removeGeneralsetting(Long id)
     */
    public void removeGeneralsetting(final Long id) {
        getHibernateTemplate().delete(getGeneralsetting(id));
    }
    
    public Generalsetting getGeneralsettingBySysIdKey(String sysId, String key)
    {
        if (sysId.equals("") && key.equals("")) {
            List list = getHibernateTemplate().find("from Generalsetting");
            Generalsetting gs2= new Generalsetting();
            if(list.size()>0)
            {
            	gs2=(Generalsetting)list.get(0);
            }
            return gs2;
        } else {
        	final Generalsetting gs = new Generalsetting();
        	gs.setSysId(sysId);
        	gs.setName(key);
            // filter on properties set in the generalsetting
            HibernateCallback callback = new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    Example ex = Example.create(gs).ignoreCase().enableLike(MatchMode.ANYWHERE);
                    return session.createCriteria(Generalsetting.class).add(ex).list();
                }
            };
            List list =(List) getHibernateTemplate().execute(callback);
            Generalsetting gs2= new Generalsetting();
            if(list.size()>0)
            {
            	gs2=(Generalsetting)list.get(0);
            }
            return gs2;
        }
    }

}
